package com.authine.cloudpivot.ext.utils;

import java.math.BigDecimal;

/**
 * Created by 张天赐 on 2018/4/14.
 * 财务计算
 */
public class FinancialCalculationUtils {
    /**
     * 根据未税计算含税单价
     */
    public static BigDecimal calculatePriceByPretaxPrice(BigDecimal pretaxPrice, BigDecimal taxRate) {
        return BigDecimalUtils.round(BigDecimalUtils.mul(pretaxPrice, taxRate.add(new BigDecimal(1))), 16);
    }

    /**
     * 根据未税单价计算含税小计
     */
    public static BigDecimal calculateTotalByPretaxPrice(BigDecimal pretaxPrice, BigDecimal taxRate, BigDecimal num) {
        return BigDecimalUtils.round(BigDecimalUtils.mul(calculatePriceByPretaxPrice(pretaxPrice, taxRate), num), 2);
    }

    /**
     * 根据未税单价计算未税小计
     */
    public static BigDecimal calculatePretaxTotalByPretaxPrice(BigDecimal pretaxPrice, BigDecimal num) {
        return BigDecimalUtils.round(BigDecimalUtils.mul(pretaxPrice, num), 2);
    }

    /**
     * 根据未税单价计算税额
     */
    public static BigDecimal calculateTaxPayableByPretaxPrice(BigDecimal pretaxPrice, BigDecimal taxRate, BigDecimal num) {
        return BigDecimalUtils.round(BigDecimalUtils.sub(calculateTotalByPretaxPrice(pretaxPrice, taxRate, num), calculatePretaxTotalByPretaxPrice(pretaxPrice, num)), 2);
    }

    /**
     * 根据未税运费计算含税运费
     */
    public static BigDecimal calculateFreightByPretaxFreight(BigDecimal pretaxFright, BigDecimal taxRateFreight) {
        return BigDecimalUtils.round(BigDecimalUtils.mul(pretaxFright, taxRateFreight.add(new BigDecimal(1))), 2);
    }

    /**
     * 根据未税运费计算运费税额
     */
    public static BigDecimal calculatePayableFreightByPretaxFreight(BigDecimal pretaxFright, BigDecimal taxRateFreight) {
        return BigDecimalUtils.round(BigDecimalUtils.sub(calculateFreightByPretaxFreight(pretaxFright, taxRateFreight), pretaxFright), 2);
    }

    /**
     * 根据未税单价和未税运费计算含税总计
     */
    public static BigDecimal calculateAmountByPretaxPriceAndPretaxFreight(BigDecimal pretaxPrice, BigDecimal pretaxFright, BigDecimal taxRate, BigDecimal taxRateFreight, BigDecimal num) {
        return BigDecimalUtils.round(BigDecimalUtils.add(calculateTotalByPretaxPrice(pretaxPrice, taxRate, num), calculateFreightByPretaxFreight(pretaxFright, taxRateFreight)), 2);
    }

    /**
     * 根据未税单价和未税运费计算未税总计
     */
    public static BigDecimal calculatePretaxAmountByPretaxPriceAndPretaxFreight(BigDecimal pretaxPrice, BigDecimal pretaxFright, BigDecimal num) {
        return BigDecimalUtils.round(BigDecimalUtils.add(calculatePretaxTotalByPretaxPrice(pretaxPrice, num), pretaxFright), 2);
    }


//    public static void main(String[] args) {
//        double num = 1256;
//        double v1 = FinancialCalculationUtils.calculatePriceByPretaxPrice(12.456123456945, 0.17);
//        System.out.println("根据未税单价价 " + num + " 计算 含税单价:" + v1);
//        double v2 = FinancialCalculationUtils.calculatePretaxTotalByPretaxPrice(12.456123456945, num);
//        System.out.println("根据未税单价 " + num + " 计算 未税小计:" + v2);
//        double v3 = FinancialCalculationUtils.calculateTotalByPretaxPrice(12.456123456945, 0.17, num);
//        System.out.println("根据未税单价 " + num + " 计算 含税小计:" + v3);
//        double v4 = FinancialCalculationUtils.calculateTaxPayableByPretaxPrice(12.456123456945, 0.17, num);
//        System.out.println("根据未税单价 " + num + " 计算 税额:" + v4);
//        double v5 = FinancialCalculationUtils.calculateFreightByPretaxFreight(19.5456178, 0.17);
//        System.out.println("根据未税运费 " + num + " 计算 含税运费:" + v5);
//        double v6 = FinancialCalculationUtils.calculatePayableFreightByPretaxFreight(19.5456178, 0.17);
//        System.out.println("根据未税运费 " + num + " 计算 运费税额:" + v6);
//        double v7 = FinancialCalculationUtils.calculatePretaxAmountByPretaxPriceAndPretaxFreight(12.456123456945, 19.5456178, num);
//        System.out.println("根据未税单价 " + num + " 计算 未税总计:" + v7);
//        double v8 = FinancialCalculationUtils.calculateAmountByPretaxPriceAndPretaxFreight(12.456123456945, 19.5456178, 0.17, 0.17, num);
//        System.out.println("根据未税单价 " + num + " 计算 含税总计:" + v8);
//    }
}
